package com.three.controller;

import java.io.PrintWriter;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.three.entity.Srollcall;
import com.three.entity.User;
import com.three.service.SrollcallService;
import com.three.service.UserService;
import com.three.util.JPushUtil;

@Controller
@RequestMapping("/srollcall")
public class SrollcallController {

	@Autowired
	UserService userService;
	@Autowired
	SrollcallService srollcallService;
	private static Logger logger = Logger.getLogger(SrollcallController.class); 
	
	@RequestMapping("/findSrollcalls")
	@ResponseBody
	public List<User> findSrollcalls(Integer cid) {
		List<User> users = userService.findStudentsByCid(cid);
		for (User user : users) {
			int uid = user.getId();
			user.setSrollcall(srollcallService.findSrollcallByUcid(uid, cid));
		}
		return users;
	}
	
	@RequestMapping("/findSrollcall")
	@ResponseBody
	public Srollcall findSrollcall(Integer uid,
			Integer cid) {
		Srollcall srollcall = srollcallService.findSrollcallByUcid(uid, cid);
		return srollcall;
	}
	
	/**
	 * 考勤签到(学生)
	 * @param uid
	 * @param cid
	 * @param rollcallCode
	 * @param out
	 */
	@RequestMapping("/sign")
	@ResponseBody
	public void sign(Integer uid,
			Integer cid,
			String rollcallCode,
			PrintWriter out) {
		String result  =null;
		result = srollcallService.sign(uid, cid, rollcallCode);
		out.write(result);
		out.close();
		if ("success".equals(result)) {
			JPushUtil.updateRollcallNum(cid+"");
		}
	}
	
	/**
	 * 修改考勤状态
	 * @param cid
	 * @param rollcallName
	 * @param ucid
	 * @param status
	 * @param out
	 */
	@RequestMapping("/updateRollcallState")
	@ResponseBody
	public void updateRollcallState(Integer cid,
			String rollcallName,
			Integer ucid,
			Integer oStatus,
			Integer uStatus,
			PrintWriter out) {
		String result  =null;
		result = srollcallService.updateRollcallState(
				cid, rollcallName, ucid, oStatus,uStatus);
		out.write(result);
		out.close();
	}
	
	
}
